import { createRouter, createWebHistory } from 'vue-router'
import login from '@/views/login.vue'
import Main from '@/views/main/index.vue'
import Home from '@/views/main/home.vue'
import EmailServer from '@/views/main/emailServer.vue'
import Accounts from '@/views/main/accountManagement.vue'
import { ElMessage } from 'element-plus'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component:login
    },
    {
      path:'/',
      redirect:'/main'
    },
    {
      path: '/main',
      component: Main,
      redirect:'/main/home',
      children: [
        {
          path: 'home',
          component:Home
        },
        {
          path: 'email_server',
          component:EmailServer
        },
        {
          path: 'accounts',
          component:Accounts
        }
      ]
    }
  ],
})


router.beforeEach((to) => {
  const obj = localStorage.getItem('USER_INFO');
  if (to.path === '/login') {
    return true
  } else {
    if (!obj) {
      ElMessage.error("请先登录")
      return '/login'
    } else {
      return true
    }
  }
})
export default router
